package edu.uoc.tdp.pac4.only5.gui.servidor;

import edu.uoc.tdp.pac4.only5.manager.BookingManager;
import edu.uoc.tdp.pac4.only5.manager.DatabaseConnectionManager;
import edu.uoc.tdp.pac4.only5.manager.DiskManager;
import edu.uoc.tdp.pac4.only5.manager.LogManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.manager.UserManager;
import edu.uoc.tdp.pac4.only5.manager.impl.BookingManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.DataBaseConnectionManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.DiskManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.LogManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.PaymentManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.ResourceManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.SpaceManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.UserManagerImpl;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;

/**
 * @author Rafeal Correal i Josep Monclus
 */
public class Servidor extends javax.swing.JFrame {

    /**
     * Crea un nova pantalla Servidor.
     * Permet arracar i aturar el sevidor.
     */
    public Servidor() {
        initComponents();
        String tituloventana = "eEspais Servidor";
        setTitle(tituloventana);
        jLabel1.setText("Pendent d'iniciar el servidor RMI: eEspais");
        jButton1.setText("Iniciar Servidor");
        jButton2.setText("Aturar Servidor");
        jButton1.setEnabled(true);
        jButton2.setEnabled(false);
        setLocation(100, 100);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(26, 26, 26).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(layout.createSequentialGroup().addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE).addGap(11, 11, 11).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)).addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /*
     * Arrenca el servidor
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try {
            BookingManager bookingManager = new BookingManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            DiskManager diskManager = new DiskManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            LogManager logManager = new LogManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            ResourceManager resourceManager = new ResourceManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            SpaceManager spaceManager = new SpaceManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            UserManager userManager = new UserManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            PaymentManager paymentManager = new PaymentManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            
            Naming.rebind("rmi://localhost/BookingManager", bookingManager);
            Naming.rebind("rmi://localhost/DiskManager", diskManager );
            Naming.rebind("rmi://localhost/LogManager", logManager );
            Naming.rebind("rmi://localhost/ResourceManager", resourceManager );
            Naming.rebind("rmi://localhost/SpaceManager", spaceManager);
            Naming.rebind("rmi://localhost/UserManager", userManager);
            Naming.rebind("rmi://localhost/PaymentManager", paymentManager);
            jLabel1.setText("!!!Servidor iniciat correctament!!!");
            jButton1.setEnabled(false);
            jButton2.setEnabled(true);
        } catch (RemoteException e) {
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog //  
                    (null, "No s'ha pogut conectar amb el registre RMI.", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog //      
                    (null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /*
     * Atura el servidor i tanca la finestra.
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        dispose();
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    /**
     * Obre la finestra del servidor
     */
    public static void main(String args[]) {
        new Servidor().setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
